	var serverData = {};
    var	serverTable;
    jQuery(document).ready(function () {
    	serverTable=  jQuery('#dataTable').DataTable({
    		          "serverSide": true,
    		          "ordering": false,
			            "ajax": {
			                "dataType": "json",
			                "type": "get",
			                "cache": false,
			                "url":window.rootPath+ '/ui/afterSaleTicket/getpage',
			                "data": function (d) {
			                    return $.extend({}, d, serverData);
			                }
			            },
			            columns: [
			                {
			                	data: null,
			                	defaultContent: ""
			                },
			                {
			                    data: null,
			                    defaultContent: "",
			                    render: function (data, type, full, meta) {
			                        var operate =' <input type="checkbox" class="checkchild" />';
			                        return operate;

			                    }
			                },
			                {
			                	data: null,
			                	 render: function (data, type, full, meta) {
				                        var dOut;
				                        if(data.isSubmit=="0"){
				                            dOut = '<td class="center" ><span class="badge label-pill label-danger">未提交</span></td>';
				                        }     
				                        else if(data.isSubmit=="1") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-danger">退回</span></td>';
				                        }   
				                        else if(data.isSubmit=="2") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-primary">已提交</span></td>';
				                        } 
				                        else if(data.isSubmit=="3") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-primary">已派遣</span></td>';
				                        } 
				                        else if(data.isSubmit=="4") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-primary">跟踪中</span></td>';
				                        } 
				                        else if(data.isSubmit=="5") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-primary">待回访</span></td>';
				                        } 
				                        else if(data.isSubmit=="6") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-success">已完成</span></td>';
				                        }
				                        return dOut;
				                    }
			                },
			                {
		                	    data: null,
		                	    render : function(data,type, full,meta) {
									var dOut;
									if (data.ticketState == '审批中') {
										dOut = '<td class="center" ><span class="badge label-pill label-primary">审批中</span></td>';
									} else if (data.ticketState == '驳回') {
										dOut = '<td class="center" ><span class="badge label-pill label-danger">驳回</span></td>';
									} else if (data.ticketState == '通过') {
										dOut = '<td class="center" ><span class="badge label-pill label-success">通过</span></td>';
									} else if (data.ticketState == '未提交') {
										dOut = '<td class="center" ><span class="badge label-pill label-default">未提交</span></td>';
									} else if (data.ticketState == '已提交') {
										dOut = '<td class="center" ><span class="badge label-pill label-warning">已提交</span></td>';
									}else if(data.ticketState==null){
										dOut='';
									}
									return dOut;
								}
		                    },
		                    {
			                	data:'afterSaleType'
			                },
			                {
			                    data: 'manageCode'
			                },
		                    {
			                	data:'proposer'
			                },
			                {
			                	data:'superior'
			                },
			                {
			                	data:'createTime',
			                	render: function (data, type, full, meta) {
		                            if (data)
		                                return moment(data).format('YYYY/MM/DD');
		                            else
		                                return '';
		                       }
			                },
			                {
			                	data:'submiter.empLogin.userName'
			                },
			                
			                {
			                    data: 'unitName',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 7) ? '<font title="'+data+'">'+data.substring(0,7)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                    data: 'contractCode'
			                },
			                
			                {
			                	data:'linkMan'
			                },
			                {
			                	data:'linkManPhone'
			                },
			                {
			                	data:'serviceTime',
			                	render: function (data, type, full, meta) {
			                            if (data)
			                                return moment(data).format('YYYY/MM/DD');
			                            else
			                                return '';
			                     }
			                },
			                {
			                	data:'deviceCode'
			                },
			                {
			                	data:'deviceName',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 6) ? '<font title="'+data+'">'+data.substring(0,6)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data:'deviceNumber',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data: 'questionDetail',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
									}
								}
			                },
			                {
				                data: 'attachNumber',
				                 render: function (data, type, full, meta) {
				                 var dOut;
				                 if(data==null){
				                   dOut='<td class="center" ><span class="badge label-pill label-primary">'+0+'</span></td>'
				                 }else{
				                   dOut='<td class="center" ><span class="badge label-pill label-primary">'+data+'</span></td>'
				                 }
				                   return dOut;
				                 }
				                    
				           }
			           ],
			            "drawCallback": function (oSettings) {
			                var k = 0;
			                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
			                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
			                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
			                    k++;
			                }
			            }
			        });

			    });
	
               //编辑 
    		   jQuery(".editBtn").on('click',function(){
    			   var roleName = jQuery("#roleName").val();
    			   var userId = jQuery("#userId").val();
    			   var selects = getSelect("#dataTable", 1);
    			   if (roleName == "系统管理员" || userId == selects[0].user.id){
    				   if (selects.length > 1) {
       					   message('一次只能编辑一条记录');
       				   } else if (selects.length == 0) {
       					message('请选择要编辑的记录');
       				   } else if (selects.length == 1) {
	       					var id = selects[0].id;
	       					var isSubmit = selects[0].isSubmit;
	       					if(isSubmit==0 ||isSubmit==1){
	       						url=window.rootPath+"/ui/afterSaleTicket/edit"+"?ticketId="+id;
	       						window.location=url;
	       					}
	       					else{
	       						message('已提交的申请不能编辑')
	       					}
       				  }   
    			   }else{
    				   message('只能编辑自己的申请')
    			   } 
    			});
    			
    			//删除
    			jQuery(".delBtn").on('click', function() {
    				var selects = getSelect("#dataTable", 1);
    			    if (selects.length == 0) {
    					message('请选择要删除的记录');
    			    }
    				else{
    					var flag = 0;
   					    for(var i = 0; i < selects.length; i++){
	   						 var isSubmit = selects[0].isSubmit;
	   						 if(isSubmit>1){
	   							 message('有已经提交的服务单，不能删除');
	   							 flag = 1;
	   							 return; 
   						     }
   						     
   					    }
   						var url = window.rootPath + "/ui/afterSaleTicket/delete";
	    				delSelect(selects, url, serverTable);
   					 }
    			});
    
    			// 提交按钮
    			jQuery(".submitBtn").on('click',function() {
    					var selects = getSelect("#dataTable", 1);
    								if (selects.length == 0) {
    									message('请选择要提交的记录');
    								} else if (selects.length > 0) {
    									var flag = 0;
    									for (var i = 0; i < selects.length; i++) {
    										var isSubmit = selects[i].isSubmit;
    										isSubmit=parseInt(isSubmit);
    										var submiterId=selects[i].submiter.id;
    										var userId = jQuery("#userId").val();
    										if (isSubmit>1) {
    											message('有已经提交的服务单，不能再提交');
    											flag = 1;
        										return;
    										}
    										if(submiterId!=userId){
    											message('不是服务单的提交人，不能提交');
    											return;
    										}
    										
    									}
    									$.springLemonDialog({
    										content : '确定提交' + selects.length + "条记录",
    										clickButton : function(sender, modal, index) {
    													if (index == 0) {
    														for (var i = 0; i < selects.length; i++) {
    															var id = selects[i].id;
    															delTask(id,"售后申请待提交");
    															var d = {
    																"isSubmit" : 2
    															}
    															var url = window.rootPath+ '/api/afterSaleTickets/'+ id;
    															jQuery.ajax({
    																		url :encodeURI(url),
    																		type :'patch',
    																		data :JSON.stringify(d),
    																		dataType: 'json',
    																		contentType : 'application/json;charset=utf-8',
    																		success: function() {
    																			serverTable.draw(false);
    																		}
    															});
    															var url1 =window.rootPath +"/ui/afterSaleTicket/saveAdviceAndBack";
    															var d1={
    																	"afterSaleTicket":{
    																		"id":id		
    																	}		
    															};
    															jQuery.ajax({
    																url :encodeURI(url1),
    																type :'post',
    																data :JSON.stringify(d1),
    																dataType : 'json',
    																contentType : 'application/json;charset=utf-8',
    																success: function(data) {
    																	
    																}
    															});
    														}
    													}
    													$(this).closeDialog(modal);
    												}
    											})
    								    }
    							});


    			
    			//查看详情
    			jQuery(".detailBtn").on('click',function(){
    				var selects = getSelect("#dataTable", 1);
    				if (selects.length > 1) {
    					$.springLemonDialog({
    						content : '一次只能查看一条记录',// 确认文本
    						clickButton : function(sender, modal, index) {// 按钮事件处理
    							$(this).closeDialog(modal);
    						}
    					});
    				} else if (selects.length == 0) {
    					$.springLemonDialog({
    						content : '请选择要查看的记录',// 确认文本
    						clickButton : function(sender, modal, index) {// 按钮事件处理
    							$(this).closeDialog(modal);
    						}
    					});
    				} else if (selects.length == 1) {
    					if(selects[0].isSubmit==0){
    						$.springLemonDialog({
        						content : '未提交的记录没有详情',// 确认文本
        						clickButton : function(sender, modal, index) {// 按钮事件处理
        							$(this).closeDialog(modal);
        						}
        					});
    					}
    					else{
    					var id = selects[0].id;
    					var afterSaleType = selects[0].afterSaleType;
    						url=window.rootPath+"/ui/afterSaleTicket/ticketDetail"+"?ticketId="+id+"&afterSaleType="+afterSaleType;
    						window.location=url;
    					}
    				}
    			});
			    //查询
			    jQuery(".searchBtn").on('click',function(){
			    	serverData.key=jQuery("#keyword").val();
			    	var isSubmit=jQuery('#ticketState').val();
			    	var afterSaleType=jQuery('#afterSaleType').val();
			    	serverData.isSumbit=isSubmit;
			    	serverData.afterSaleType=afterSaleType;
			    	serverTable.draw();
			    });
			    
			    //撤消
			    jQuery(".cancelBtn").on('click',function(){
			    	jQuery("#keyword").val('');
			    	jQuery("#ticketState").val('');
			    	jQuery("#afterSaleType").val('');
			    	serverData.key='';
			    	serverData.isSumbit='';
			    	serverData.afterSaleType='';
			    	serverTable.draw();
			    });
			    jQuery('#ticketState').change(function(){
			    	serverData.key=jQuery("#keyword").val();
			    	var isSubmit=jQuery('#ticketState').val();
			    	var afterSaleType=jQuery('#afterSaleType').val();
			    	serverData.isSumbit=isSubmit;
			    	serverData.afterSaleType=afterSaleType;
			    	serverTable.draw();
				});
			    jQuery('#afterSaleType').change(function(){
			    	serverData.key=jQuery("#keyword").val();
			    	var isSubmit=jQuery('#ticketState').val();
			    	var afterSaleType=jQuery('#afterSaleType').val();
			    	serverData.isSumbit=isSubmit;
			    	serverData.afterSaleType=afterSaleType;
			    	serverTable.draw();
			    });
			    //列表全选
			    $("#checkAll").click(function() {
			    	var check = $(this).prop("checked");
			    	$(".checkchild").prop("checked", check);
			    });
			    
			    
			    
			    